#include "user_include.h"

#define GPIO1_READ() HAL_GPIO_ReadPin(GPIOH, GPIO_PIN_3)
#define GPIO2_READ() HAL_GPIO_ReadPin(GPIOH, GPIO_PIN_2)
#define GPIO3_READ() HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13)

#define BUTTON_NUM 1
#define BUTTON_UNPRESSED 0
#define BUTTON1 1
#define BUTTON2 2
#define BUTTON3 3
#define BUTTON4 4

extern void button_init(void);
extern void button_task(void);

struct driver_button_struct_def driver_button_struct = {
    .key_value = 0,
    .init = button_init,
    .task = button_task,
};

/****************************************************************************
 * name: button_init
 * function: button init function set the io as input
 * in parameters: no
 * out parameters: no
 ****************************************************************************/
static void button_init(void)
{
}

/****************************************************************************
 * name: board_button_read
 * function: read button value,no press return 1, pressed return 1
 * in parameters: which_button:  whice io should be detect
 * out parameters: no press return 1, pressed return 1
 ****************************************************************************/
static uint8_t driver_button_read(void) // read button value
{
    uint8_t button = 0;

#if 1
    if (GPIO1_READ() == 0)
    {
        button = BUTTON1;
    }
    else if (GPIO2_READ() == 0)
    {
        button = BUTTON2;
    }
    else if (GPIO3_READ() == 0)
    {
        button = BUTTON3;
    }
    else
    {
        button = BUTTON_UNPRESSED;
    }
    return button;
#else
    if (GPIO1_READ() == 0)
    {
        button = BUTTON1;
    }
    else if (GPIO3_READ() == 0)
    {
        button = BUTTON2;
    }
    else
    {
        button = BUTTON_UNPRESSED;
    }
    return button;
#endif
}

/****************************************************************************
 * name: board_button_read
 * function: The application layer function, no need change when change mcu
 * in parameters: no
 * out parameters: no
 ****************************************************************************/
static void button_task(void)
{
    static uint8_t state = 0;
    static uint8_t button = 0;
    static uint8_t button_clear_count = 0;
    static uint16_t count = 0;
    uint16_t count_temp = 0;
    uint8_t temp = 0;

    while (1)
    {
        osDelay(10);
        button_clear_count++;
        if (button_clear_count >= 20) // if no handle is 1s ,clean button flag
        {
            button_clear_count = 0;
            driver_button_struct.key_value = 0x00;
        }
        switch (state)
        {
        case (0):
        {
            button = driver_button_read();
            if (button != BUTTON_UNPRESSED) /*button has been pressed*/
            {
                state = 2;
                count = 0;
            }
        }
        break;
        case (2):
        {
            count++;
            if (count > 10) /* avoid vibrating */
            {
                temp = driver_button_read();
                if (temp != BUTTON_UNPRESSED) // button has been pressed
                {
                    if (temp != button)
                    {
                        state = 0;
                    }
                    else
                    {
                        state = 3;
                        count = 0;
                    }
                }
                else
                {
                    state = 0;
                }
            }
        }
        break;
        case (3):
        {
            count++;
            temp = driver_button_read();

            if (temp == BUTTON_UNPRESSED) // button Unpressed
            {
                count = 0;
                state = 5;
            }
            if (count >= 100) // when Temp0>=5S set LongPressBit
            {
                if (button == BUTTON1)
                    driver_button_struct.key_value = BUTTON1_LONG_PRESS_FLAG;
                else if (button == BUTTON2)
                    driver_button_struct.key_value = BUTTON2_LONG_PRESS_FLAG;
                else if (button == BUTTON3)
                    driver_button_struct.key_value = BUTTON3_LONG_PRESS_FLAG;
                else if (button == BUTTON4)
                    driver_button_struct.key_value = BUTTON4_LONG_PRESS_FLAG;
                button_clear_count = 0;
                state = 4;
            }
        }
        break;
        case (4): // wait unpress
        {
            temp = driver_button_read();
            if (temp == BUTTON_UNPRESSED) // button Unpressed
            {
                count = 0;
                state = 0;
            }
        }
        break;
        case (5):
        {
            count++;
            if (count >= 1)
            {
                if (button == BUTTON1)
                    driver_button_struct.key_value = BUTTON1_PRESS_FLAG;
                else if (button == BUTTON2)
                    driver_button_struct.key_value = BUTTON2_PRESS_FLAG;
                else if (button == BUTTON3)
                    driver_button_struct.key_value = BUTTON3_PRESS_FLAG;
                else if (button == BUTTON4)
                    driver_button_struct.key_value = BUTTON4_PRESS_FLAG;
                button_clear_count = 0;
                state = 0;
            }
            else
            {
                temp = driver_button_read();
                if (temp != BUTTON_UNPRESSED) // button Unpressed
                {
                    count = 0;
                    state = 6;
                }
            }
        }
        break;
        case (6):
        {
            temp = driver_button_read();
            if (temp == BUTTON_UNPRESSED) // button Unpressed
            {
                if (button == BUTTON1)
                    driver_button_struct.key_value = BUTTON1_DOUBLE_PRESS_FLAG;
                else if (button == BUTTON2)
                    driver_button_struct.key_value = BUTTON2_DOUBLE_PRESS_FLAG;
                else if (button == BUTTON3)
                    driver_button_struct.key_value = BUTTON3_DOUBLE_PRESS_FLAG;
                else if (button == BUTTON4)
                    driver_button_struct.key_value = BUTTON4_DOUBLE_PRESS_FLAG;
                button_clear_count = 0;
                state = 0;
            }
        }
        break;
        default:
        {
            state = 0;
        }
        break;
        }
    }
}